<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/30
 * Time: 17:57
 */

namespace App\Http\Controllers\Backstage;


use App\Http\Controllers\Controller;
use App\Model\Backstage\CustomerFollowSourde;
use App\Model\Backstage\CustomersFollow;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Validator;
use App\Tool\Error;

/**
 * Class CustomerFollowController
 * @package App\Http\Controllers\Backstage
 * 客源跟进控制器
 */
class CustomerFollowController extends Controller
{

    //添加客源跟进
    public function addCustomerFollow(Request $request, CustomersFollow $customersfollow, CustomerFollowSourde $customerfollowsourde)
    {
        $cfsid = true;
        $params = $request->all();
        $validator = Validator::make($params, [
            'cf_c_id'         => 'required',
            'cf_fllow_type'   => 'required',
            'cf_content'      => 'required',
        ]);
        if ($validator->fails()) {
            return $this->error(Error::PARAM_MISSING);
        }
        //开启事务
        DB::beginTransaction();
        if($params['cf_fllow_type'] == 3){
            $cfid = $customersfollow->addCustomersFollow($params['cf_c_id'], $params['cf_fllow_type'], $params['cf_content'], $params['cf_room_id']);
        }else{
            $cfid = $customersfollow->addCustomersFollow($params['cf_c_id'], $params['cf_fllow_type'], $params['cf_content'],'');
        }
            if(isset($params['cfs_remind_time'])){
                  $cfs_user_id = explode(',', $params['cfs_user_id']);
                //添加提醒跟进范围  提醒时间有的话   添加提醒范围
                if (isset($params['cfs_remind_time'])){
                    $cfsid = $customerfollowsourde->addCustomerFollowSourde($params['cfs_type_id'],$cfid, $params['cfs_type'], $cfs_user_id, $params['cfs_remind_time'], $params['cfs_content']);
                }else {
                    $cfsid = true;
                }
            }
        if($cfid && $cfsid){
            DB::commit();
            return $this->success('ok', '跟进添加成功');
        } else {
            DB::rollback();
            return $this->error(Error::ADD_ERROR);
        }
    }

    /*
     * 删除客源跟进
     * Wu Ning
     */
    public function delCustomerFollow(Request $request, CustomersFollow $customersfollow)
    {
        $followId = $request->all('cf_id');
        $rel = $customersfollow->delFollow($followId);
        if($rel){
            return $this->success('ok', '删除成功');
        }else{
            return $this->error(Error::DEL_ERROR);
        }

    }
}





















